package com.gxl.service.impl;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.gxl.common.Constants;
import com.gxl.mapper.AccountMapper;
import com.gxl.pojo.Account;
import com.gxl.service.AccountService;
import com.gxl.util.MyBatisUtil;

public class AccountServiceImpl implements AccountService {

	@Override
	public String transfer(Account accout, Account accin) throws IOException {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		AccountMapper mapper = sqlSession.getMapper(AccountMapper.class);
		Account selByAccPwd = mapper.selByAccPwd(accout);
		if(selByAccPwd != null){
			if(selByAccPwd.getBalance() >= accout.getBalance()){
				Account selByAccName = mapper.selByAccName(accin);
				if(selByAccName != null){
					accin.setBalance(accout.getBalance());
					mapper.updBalanceByAccount(accin);
					accout.setBalance(-accout.getBalance());
					mapper.updBalanceByAccount(accout);
					return Constants.TRANSFER_SUCCESS;
				}else{
					return Constants.ACCOUNT_NAME_NOT_MATCH;
				}
			}else{
				return Constants.BALANCE_NOT_ENOUGH;
			}
		}else{
			return Constants.ACCOUNT_PASSWORD_NOT_MATCH;
		}
	}
	
}
